namespace EyeOpen.Web.Hosting
{
	using System;
	using System.Collections.Generic;
	using System.Web.Hosting;

	public class EmbeddedVirtualPathProvider 
		: VirtualPathProvider
	{
		private readonly IDictionary<string, Tuple<string, string>> virtualPathDictionary;

		public EmbeddedVirtualPathProvider(VirtualPathInitializer virtualPathInitializer)
			: this(virtualPathInitializer.Initialize())
		{
		}

		public EmbeddedVirtualPathProvider(IDictionary<string, Tuple<string, string>> virtualPathDictionary)
		{
			this.virtualPathDictionary = virtualPathDictionary;
		}

		public override bool FileExists(string virtualPath)
		{
			return base.FileExists(virtualPath) || ResourceFileExists(virtualPath);
		}

		public override VirtualFile GetFile(string virtualPath)
		{
			return 
				!base.FileExists(virtualPath) ?
					new EmbeddedVirtualFile(virtualPath, virtualPathDictionary) : base.GetFile(virtualPath);
		}

		public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, System.DateTime utcStart)
		{
			return null;
		}

		private bool ResourceFileExists(string virtualPath)
		{
			return
				virtualPathDictionary.ContainsKey(virtualPath);
		}
	}
}